﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ProvideInfo : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString().Trim();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddate();


        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString().Trim();
        string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
        string add = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
        string phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim();
        users tmp = new users();
        tmp.id = id;
        tmp.name = name;
        tmp.add = add;
        tmp.phone= phone;
        tmp.Updateoneuser4(tmp);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString().Trim();
        users us = new users();
        us.id = id;
        us.delete4(us);
        Binddate();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Binddate();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Binddate();
    }
    protected void Binddate()
    {
        users us = new users();
        GridView1.DataSource = us.GetAllUsers5(us);
        GridView1.DataBind();
    }
    protected void btnADD_Click(object sender, EventArgs e)
    {
        string id = txtID.Text.Trim();
        string name = txtName.Text.Trim();
        string add= txtAdd.Text.Trim();
        string phone = txtAdd.Text.Trim();
        users us = new users();
        us.name = name;
        us.id = id;
        us.add = add;
        us.phone = phone ;
        int result = us.isExist(us);
        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {
            conn.Open();
            string sql = "insert into ProvideInfo (ProvideID,ProvideName,ProvideAdd,ProvidePhone) VALUES ('" + id + "','" + name + "','" + add + "','" + phone  + "')";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            Response.Write("<script>alert(\"供货商信息登记成功!\")</script>");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
}